package com.crm.controller.system;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.common.result.TableDataInfo;
import com.crm.controller.BaseController;
import com.crm.DTO.BasePageQuery;
import com.crm.common.result.AjaxResult;
import com.crm.common.utils.StringUtils;
import com.crm.entity.system.SysDictData;
import com.crm.service.system.ISysDictDataService;
import com.crm.service.system.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController extends BaseController
{
    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    /***
     * 分页查询
     * @param dictData
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(SysDictData dictData, BasePageQuery pageQuery)
    {
        Page<SysDictData> list= dictDataService.selectDictDataList(dictData,pageQuery);
        return getDataTable(list);
    }


    /**
     * 查询字典数据详细
     */

    @GetMapping(value = "/{dictCode}")
    public AjaxResult getInfo(@PathVariable Long dictCode)
    {
        return success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType)
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<SysDictData>();
        }
        return success(data);
    }

    /**
     * 新增字典类型
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDictData dict)
    {
       //TODO dict.setCreateBy(getUsername());
        return toAjax(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */


    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysDictData dict)
    {
        //TODO dict.setUpdateBy(getUsername());
        return toAjax(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */

    @DeleteMapping("/{dictCodes}")
    public AjaxResult remove(@PathVariable Long[] dictCodes)
    {
        dictDataService.deleteDictDataByIds(dictCodes);
        return success();
    }
}
